<template>
	<view class="login">
		<u-icon name="arrow-left" color="white" size="24"></u-icon>
		<view class="card">
			<text class="tit" v-if="!changeFlag">登 录</text>
			<text class="tit" v-if="changeFlag">注 册</text>
			<view class="input-box">
				<u-icon name="account" size="20"></u-icon>
				<u-input v-model="username" :type="type" :border="border" placeholder="账号"  />
			</view>
			<view class="input-box">
				<u-icon name="lock" size="20"></u-icon>
				<u-input v-model="password" :type="type" :border="border" placeholder="密码"  />
			</view>
			<view class="input-box" v-if="changeFlag">
				<u-icon name="lock" size="20"></u-icon>
				<u-input v-model="rpwd" :type="type" :border="border" placeholder="确认密码"  />
			</view>
			<u-button type="success" v-if="!changeFlag" @click="Login()">登录</u-button>
			<u-button type="success" v-if="changeFlag" @click="SetUser()">注册</u-button>
			<view class="forget">
				<text @click="changeFlag = !changeFlag" v-if="!changeFlag">注册</text>
				<text @click="changeFlag = !changeFlag" v-if="changeFlag">去登录</text>
				<text>忘记密码？</text>
			</view>
			<view class="wx">
				<u-icon size="30" color="#5ccc84" name="weixin-fill"></u-icon>
			</view>
			<view class="check" v-if="!changeFlag">
					<checkbox :value="a"/><text>已阅读并同意用户协议&隐私声明</text>
			</view>
		</view>
		<u-toast ref="uToast" />
	</view>
</template>
<script>
	import { setUser, getLogin, } from "../../http/api.js"
	export default {
		data() {
			return {
				changeFlag:false,
				username:'',
				password:'',
				rpwd:'',
			};
		},
		onLoad() {
			
		},
		methods:{
		 	async SetUser(){
				let obj = {username:this.username,password:this.password,repassword:this.rpwd}
				let data = await setUser(obj);
				// console.log(data);
				if(data.code === 20000){
					uni.showToast({
							title: '注册成功!',
							icon: 'none',
							duration:3000
						})
						this.changeFlag = false;
						this.pswd = '';
						// console.log(this.changeFlag);
				}else{
					uni.showToast({
							title: data.data,
							icon: 'none',
							duration:3000
						})
				}
			},
			async Login(){
				let obj = {username:this.username,password:this.password}
				let data = await getLogin(obj);
				console.log(data);
				if(data.code === 20000){
					console.log(data.data.token)
					uni.setStorageSync('token',data.data.token);
					uni.setStorageSync('user',data.data);
					if(data.data.phone == null){
						uni.navigateTo({
							url:'../bind-phone/bind-phone'
						})
					}else{
						uni.navigateBack({
							delta:1
						})
					}
					uni.showToast({
							title: '登录成功!',
							icon: 'none',
							duration:3000
						})
				}else{
					uni.showToast({
							title: data.data,
							icon: 'none',
							duration:3000
						})
				}
			},
		}
	}
</script>

<style lang="scss">
	.login{
		height: 100%;
		background: linear-gradient(120deg,#3bfdaf,#70d6f2);
		&>.u-icon{
			padding: 30rpx;
		}
		.card{
			background-color: #fff;
			height: 76%;
			border-radius: 5% 5% 0 0;
			// margin-top: 24%;
			position: fixed;
			width: 100%;
			bottom: 0;
			padding: 60rpx;
			box-sizing: border-box;
			.tit{
				font-size: 40rpx;
			}
			.input-box{
				margin: 30rpx 0;
				position: relative;
				.u-icon{
					position: absolute;
					top: 50%;
					left: 15rpx;
					transform: translate(0,-50%);
				}
				.u-input{
					background: #F5F5F5;
					border-radius: 10rpx;
					input{
						padding: 0 60rpx;
					}
				}
			}
			.forget{
				padding: 20rpx 0;
				display: flex;
				flex-direction: row;
				justify-content: space-between;
				:first-child{
					color: #5ccc84;
				}
				:last-child{
					color: #777;
				}
			}
			.wx{
				width: 100rpx;
				height: 100rpx;
				border: 2rpx solid #5ccc84;
				border-radius: 50%;
				padding: 17rpx;
				box-sizing: border-box;
				margin: 0 auto;
			}
			.check{
				margin-top: 30rpx;
				color: #777;
				checkbox{
					transform:scale(0.6);
				}
			}
			
		}
	}
</style>
